package com.easygoods.controller;

import com.bestvike.stone.core.io.StreamReader;
import com.bestvike.stone.core.io.StreamWriter;
import com.bestvike.stone.core.serialization.XmlSerializer;
import com.bestvike.stone.spring.controller.BaseController;
import com.bestvike.stone.spring.exception.AssertUtils;
import com.bestvike.stone.spring.rest.common.CommonResponse;
import com.bestvike.stone.spring.shiro.annotation.RequiresPermissions;
import com.easygoods.external.wechat.common.bean.WeChatColor;
import com.easygoods.external.wechat.common.response.WeChatSessionResponse;
import com.easygoods.external.wechat.pay.respon.WeChatPayCoreResponse;
import com.easygoods.service.WeChatService;
import com.easygoods.shiro.auth.UserPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * Created by 许崇雷 on 2019-01-23.
 */
@RestController
@RequestMapping("/api/wechat")
public class WeChatController extends BaseController {
    @Autowired
    private WeChatService weChatService;

    //获取微信 openid
    @GetMapping("/getSession")
    public CommonResponse<WeChatSessionResponse> getSession(String jscode) {
        AssertUtils.hasText(jscode, "jscode");
        final WeChatSessionResponse weChatSessionResponse = this.weChatService.getSession(jscode);
        return CommonResponse.success(weChatSessionResponse);
    }

    //获取小程序二维码
    @RequiresPermissions(UserPermissions.BUY)
    @GetMapping("/getWXACodeUnlimit")
    public void getWXACodeUnlimit(String path, Integer width, Integer r, Integer g, Integer b, HttpServletResponse response) throws IOException {
        AssertUtils.hasText(path, "path");
        AssertUtils.notNull(width, "width");

        WeChatColor lineColor;
        if (r == null || g == null || b == null) {
            lineColor = null;
        } else {
            lineColor = new WeChatColor();
            lineColor.setR(r);
            lineColor.setG(g);
            lineColor.setB(b);
        }

        this.weChatService.getWXACodeUnlimit(path, width, lineColor, response);
    }

    //微信支付回调
    @PostMapping("/payNotify")
    public void notify(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestXml;
        try (ServletInputStream inputStream = request.getInputStream();
             StreamReader reader = new StreamReader(inputStream, StandardCharsets.UTF_8)) {
            requestXml = reader.readToEnd();
        }
        //处理支付
        final WeChatPayCoreResponse coreResponse = this.weChatService.payOrder(requestXml);
        //返回
        String responseXml = XmlSerializer.serialize(coreResponse, "xml");
        response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE);
        try (ServletOutputStream outputStream = response.getOutputStream();
             StreamWriter writer = new StreamWriter(outputStream, StandardCharsets.UTF_8)) {
            writer.write(responseXml);
        } catch (Exception e) {
            this.log.warn("支付通知, 写回响应失败");
        }
    }
}
